<template>
  <div class="login-container">
    <el-form
      ref="registerForm"
      :model="registerForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <!-- 标题 -->
      <div class="title-container">
        <h3 class="title">计算机学习平台-注册界面</h3>
      </div>
      <!-- 用户名 -->
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="registerForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          tabindex="1"
        />
        <span
          style="
           position: absolute;
           top: 30px;
           left: 530px;
           color: #064425;
           font-size: 43px;
           font-weight: bold;
           width: 400px;"
        >
          这里返回登录吧！
        </span>
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="password"
          v-model="registerForm.password"
          type="password"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
        />
      </el-form-item>

      <!-- 确认密码 -->
      <el-form-item prop="repeat">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="repeat"
          v-model="registerForm.repeat"
          type="password"
          placeholder="请确认密码"
          name="repeat"
          tabindex="3"
        />
      </el-form-item>

      <!-- 验证码 -->
      <el-row>
        <el-col :span="16">
          <el-form-item prop="code">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              ref="code"
              v-model="registerForm.code"
              class="yuan"
              placeholder="请输入验证码"
              name="code"
              type="text"
              tabindex="3"
              @keyup.enter.native="handleRight"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <img :src="url" alt="加载失败" width="150px" height="50px" style="cursor:pointer; position: absolute; margin-top: 58px; margin-left:-367px;margin-bottom: 7px" @click="handleCheckCode">
        </el-col>
      </el-row>

      <!-- 登录按钮 -->
      <div style="height: 40px; margin-bottom: 30px">
        <el-button
          class="loginbutton"
          :loading="loading"
          type="primary"
          style="width: 20%; float: left;margin-right: 400px"
          @click.native.prevent="handleRight"
        >确认</el-button>
        <el-button
          class="returnbutton"
          :loading="loading"
          type="success"
          style="width: 20%; float: left;position: absolute;bottom: 200px"
          @click.native.prevent="handleBack"
        >返回登录</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { register } from '@/api/system/user'
import {
  getCode
} from '@/api/system/user'
export default {
  name: 'Register',
  data() {
    const validateRepeat = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      url: null,
      registerForm: {
        username: '',
        password: '',
        repeat: '',
        key: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        repeat: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { trigger: 'blur', validator: validateRepeat }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      loading: false
    }
  },
  created() {
    this.handleCheckCode()
  },
  methods: {
    // 切换验证码
    handleCheckCode() {
      getCode().then((res) => {
        this.registerForm.key = res.data.key
        this.url = res.data.img
      })
    },
    // 确定
    handleRight() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.loading = true
          register(this.registerForm).then((res) => {
            if (res.status === 200) { // 注册成功
              this.$message.success(res.message)
              // 跳回登录界面
              this.$router.push({
                path: '/login',
                query: {
                  username: this.registerForm.username,
                  password: this.registerForm.password
                }
              })
              this.loading = false
            } else { // 注册失败
              this.$message.error(res.message)
              // 刷新验证码
              this.handleCheckCode()
              this.loading = false
            }
          })
        } else {
          console.log('不允许提交!')
          return false
        }
      })
    },
    // 返回登录界面
    handleBack() {
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (caret-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url('~@/assets/images/background.jpg'); // 使用背景图片
  background-size: cover; // 背景图片覆盖整个容器
  background-position: center; // 背景图片居中
  background-repeat: no-repeat; // 背景图片不重复
  overflow: hidden;

  .el-input {
    //display: inline-block;
    height: 47px;
    width: 45%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      //caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #dbe9de inset !important;
        -webkit-text-fill-color: #283443 !important;
      }
    }
  }

  .el-form-item {
    border: 1.5px solid rgba(0, 94, 76, 1);
    background: transparent;
    border-radius: 5px;
    color: #454545;
    width: 370px;
    height: 45px;
  }

  .el-icon-arrow-up:before {
    content: '';
  }

  .loginbutton {
    background-color: #064425;
    width: 70px;
    height: 40px;
    border-radius: 30px;
  }

  .returnbutton {
    background-color: #064425;
    width: 70px;
    height: 40px;
    border-radius: 30px;
  }

  ::placeholder {
    color: #666; /* 将颜色调整为深灰色 */
    opacity: 1; /* 确保颜色不透明 */
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url('~@/assets/images/background.jpg'); // 使用背景图片
  background-size: cover; // 背景图片覆盖整个容器
  background-position: center; // 背景图片居中
  background-repeat: no-repeat; // 背景图片不重复
  overflow: hidden;

  .login-form {
    background-image: url('~@/assets/images/background1.png');
    background-color: rgba(255, 255, 255, 0.7);
    background-size: cover; // 背景图片覆盖整个容器
    position: relative;
    width: 60%;
    height: 500px;
    //max-width: 100%;
    padding: 50px 70px 0;
    margin: 150px auto;
    overflow: hidden;
    border-radius: 30px; // 添加圆角
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $bg;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $bg;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
